package com.freewater.parking.domain.parking;

import com.freewater.parking.domain.common.DomainEvent;
import com.freewater.parking.domain.common.Nullable;
import com.freewater.parking.domain.common.PlateNumber;
import org.apache.commons.lang3.Validate;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 入场失败事件
 */
public class CheckInFailedEvent implements DomainEvent {
    /**
     * 车牌号
     */
    private final PlateNumber plateNumber;
    /**
     * 入场时间
     */
    private final LocalDateTime checkInTime;
    /**
     * 失败原因
     */
    private final String cause;

    public CheckInFailedEvent(PlateNumber plateNumber,
                              LocalDateTime checkInTime,
                              @Nullable String cause) {
        Validate.notNull(plateNumber, "车牌号不能为空");
        Validate.notNull(checkInTime, "车辆入场时间不能为空");
        this.plateNumber = plateNumber;
        this.checkInTime = checkInTime;
        this.cause = cause;
    }

    @Override
    public LocalDateTime occurAt() {
        return this.checkInTime;
    }

    /**
     * 获取车牌号
     *
     * @return 车牌号
     */
    public PlateNumber getPlateNumber() {
        return plateNumber;
    }

    /**
     * 获取入场时间
     *
     * @return 入场时间
     */
    public LocalDateTime getCheckInTime() {
        return checkInTime;
    }

    /**
     * 获取入场失败原因
     *
     * @return 入场失败原因
     */
    public String getCause() {
        return cause;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        CheckInFailedEvent that = (CheckInFailedEvent) o;
        return Objects.equals(plateNumber, that.plateNumber)
                && Objects.equals(checkInTime, that.checkInTime)
                && Objects.equals(cause, that.cause);
    }

    @Override
    public int hashCode() {
        return Objects.hash(plateNumber, checkInTime, cause);
    }
}
